package com.tcp2.util;

import com.tcp2.frame.ServerConfig;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 * Author: <张锦煌>
 * Time: 2015-09-11 10:22
 * Copyright (C) 2015 Xiamen Yaxon Networks CO.,LTD.
 */
/*
 *读取配置文件
 */

public class LoadFile {
    //读取服务器配置文件
    public ServerConfig loadServerConfig(String fileName) throws Exception {
        PropertiesConfiguration configuration = new PropertiesConfiguration(fileName);
        ServerConfig serverConfig=new ServerConfig();
        serverConfig.setAddress(configuration.getString("Address"));
        serverConfig.setPort(configuration.getString("Port"));
        serverConfig.setDataSource(configuration.getByte("DataSource"));
        serverConfig.setTermVersion(configuration.getByte("TermVersion"));
        serverConfig.setLat(configuration.getInt("Lat"));
        serverConfig.setLon(configuration.getInt("Lon"));
        serverConfig.setSpeed(configuration.getInt("Speed"));
        return serverConfig;
    }



    //读取所有车辆信息配置文件
    public Vector loadConfigInfo(int s4) throws Exception {
        PropertiesConfiguration configuration1 = new PropertiesConfiguration();
        configuration1.setEncoding("gbk");
        configuration1.setFileName("./conf/veInfo.properties");
        configuration1.load();
        Vector vector0=new Vector();
        List<String> list0 = new ArrayList<String>();
        Iterator<String> itr=loadAuth().getKeys();
        while(itr.hasNext()){
            list0.add(itr.next());
        }
        for (int i = 0; i < s4; i++) {
            List<Object> list = new ArrayList<Object>();
            Vector vector=new Vector();
            list=configuration1.getList("sim"+i);
            list.add(list0.contains(list.get(0)) ? "离线" : "未注册");
            vector.addAll(list);
            vector0.add(vector);
        }
        return vector0;
    }

    //读取鉴权码
    public PropertiesConfiguration loadAuth() throws Exception{
        PropertiesConfiguration authconfig=new PropertiesConfiguration();
        authconfig.setFileName("./conf/authcode.properties");
        authconfig.load();
        Iterator<String> itr=authconfig.getKeys();
        while(itr.hasNext()){
            String key=itr.next();
            String values=authconfig.getString(key);
            MyQuene.putAuthcode(Long.valueOf(key), values);
        }
        return authconfig;
    }


}
